package com.ruoyi.room.controller;

import com.ruoyi.blindBoxManage.domain.BlindBoxProduct;
import com.ruoyi.blindBoxManage.service.IBlindBoxProductService;
import com.ruoyi.blindBoxManage.service.IProductTypeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.room.domain.Room;
import com.ruoyi.room.domain.RoomDrawPresale;
import com.ruoyi.room.service.IRoomDrawPresaleService;
import com.ruoyi.room.service.IRoomService;
import com.ruoyi.userManage.domain.Package;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.service.IPackageService;
import com.ruoyi.userManage.service.IUserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预售发放记录Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/room/presale")
public class RoomDrawPresaleController extends BaseController {
    @Autowired
    private IRoomDrawPresaleService roomDrawPresaleService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IBlindBoxProductService blindBoxProductService;
    @Autowired
    private IPackageService packageService;
    @Autowired
    private IProductTypeService productTypeService;

    /**
     * 查询预售发放记录列表
     */
    @PreAuthorize("@ss.hasPermi('room:presale:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoomDrawPresale roomDrawPresale) {
        startPage();
        List<RoomDrawPresale> list = roomDrawPresaleService.selectRoomDrawPresaleList(roomDrawPresale);
//        for (RoomDrawPresale drawPresale : list) {
//            User user = userService.selectUserById(drawPresale.getUserId());
//            Room room = roomService.selectRoomById(drawPresale.getRoomId());
//            BlindBoxProduct blindBoxProduct = blindBoxProductService.selectBlindBoxProductById(drawPresale.getGoodId());
//            drawPresale.setRoomName(room.getName());
//            drawPresale.setGoodName(blindBoxProduct.getName());
//            drawPresale.setGoodLevelName(productTypeService.selectProductTypeById(blindBoxProduct.getProductTypeId()).getName());
//            drawPresale.setPhone(user.getPhone());
//            drawPresale.setNickName(user.getName());
//        }
        return getDataTable(list);
    }

    /**
     * 导出预售发放记录列表
     */
    @PreAuthorize("@ss.hasPermi('room:presale:export')")
    @Log(title = "预售发放记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoomDrawPresale roomDrawPresale) {
        List<RoomDrawPresale> list = roomDrawPresaleService.selectRoomDrawPresaleList(roomDrawPresale);
        ExcelUtil<RoomDrawPresale> util = new ExcelUtil<RoomDrawPresale>(RoomDrawPresale.class);
        util.exportExcel(response, list, "预售发放记录数据");
    }

    /**
     * 获取预售发放记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('room:presale:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        RoomDrawPresale drawPresale = roomDrawPresaleService.selectRoomDrawPresaleById(id);
        User user = userService.selectUserById(drawPresale.getUserId());
        Room room = roomService.selectRoomById(drawPresale.getRoomId());
        BlindBoxProduct blindBoxProduct = blindBoxProductService.selectBlindBoxProductById(drawPresale.getGoodId());
        drawPresale.setRoomName(room.getName());
        drawPresale.setGoodName(blindBoxProduct.getName());
        drawPresale.setGoodLevelName(blindBoxProduct.getProductTypeName());
        drawPresale.setPhone(user.getPhone());
        drawPresale.setNickName(user.getName());
        return success(drawPresale);
    }

    /**
     * 新增预售发放记录
     */
    @PreAuthorize("@ss.hasPermi('room:presale:add')")
    @Log(title = "预售发放记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RoomDrawPresale roomDrawPresale) {
        return toAjax(roomDrawPresaleService.insertRoomDrawPresale(roomDrawPresale));
    }

    /**
     * 修改预售发放记录
     */
    @PreAuthorize("@ss.hasPermi('room:presale:edit')")
    @Log(title = "预售发放记录", businessType = BusinessType.UPDATE)
    @PutMapping("/grant")
    public AjaxResult grant(@RequestBody RoomDrawPresale roomDrawPresale) {
        RoomDrawPresale roomDrawPresale1 = roomDrawPresaleService.selectRoomDrawPresaleById(roomDrawPresale.getId());
        if (roomDrawPresale1.getIsGrant() == 1) {
            throw new ServiceException("已经发放过了");
        }
        BlindBoxProduct boxProduct = blindBoxProductService.selectBlindBoxProductById(roomDrawPresale.getGoodId());
        //现货,直接放入背包
        Package aPackage = new Package();
        aPackage.setUserId(roomDrawPresale.getUserId());
        aPackage.setClassId(boxProduct.getClassId());
        aPackage.setBlindBoxId(boxProduct.getBlindBoxId());
        aPackage.setBoxId(boxProduct.getBoxId());
        aPackage.setProductId(boxProduct.getId());
        aPackage.setPublicProductId(boxProduct.getPublicProductId());
        aPackage.setProductTypeId(boxProduct.getProductTypeId().longValue());
        aPackage.setProductTypeName(productTypeService.selectProductTypeById(boxProduct.getProductTypeId()).getName());
        aPackage.setProductName(boxProduct.getName());
        aPackage.setProductPicture(boxProduct.getPicture());
        aPackage.setProductPrice(boxProduct.getPrice());
        aPackage.setStatus(0);
        aPackage.setIsSplit(boxProduct.getIsSplit());
        aPackage.setIsDelivery(boxProduct.getIsDelivery());
        aPackage.setSplitCount(boxProduct.getSplitCount());
        aPackage.setFreight(boxProduct.getFreight());
        packageService.insertPackage(aPackage);
        roomDrawPresale.setIsGrant(1);
        return toAjax(roomDrawPresaleService.updateRoomDrawPresale(roomDrawPresale));
    }

    /**
     * 修改预售发放记录
     */
    @PreAuthorize("@ss.hasPermi('room:presale:edit')")
    @Log(title = "预售发放记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RoomDrawPresale roomDrawPresale) {
        return toAjax(roomDrawPresaleService.updateRoomDrawPresale(roomDrawPresale));
    }

    /**
     * 删除预售发放记录
     */
    @PreAuthorize("@ss.hasPermi('room:presale:remove')")
    @Log(title = "预售发放记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(roomDrawPresaleService.deleteRoomDrawPresaleByIds(ids));
    }
}
